package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Person;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private Scanner sc=new Scanner(System.in);
    private StudentService studentService=new StudentService();

    public void start() {
        while (true) {
            System.out.println("--------------欢迎来到学校信息管理系统-------------------");
            System.out.println("1.添加学生   2.删除学生   3.修改学生   4.查询学生   5.退出");
            System.out.println("请输入您的选择;");
            String choice=sc.next();
            switch (choice) {
                case "1":
                    AddStudent();
                    break;
                case "2":
                    System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    System.out.println("修改学生");
                    upDateStudent();
                    break;
                case "4":
                    findAllStudent();
                    System.out.println("查询学生");
                    break;
                case "5":
                    System.out.println("退出");
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    private void deleteStudent() {
        String id;
        while (true){
            System.out.println("输入id：");
            id=sc.next();
            boolean b=studentService.isExists(id);
            if (!b){
                System.out.println("id不存在，重新输入：");
            }else {
                break;
            }
        }
        studentService.deleteStudent(id);
        System.out.println("删除成功");
    }

    private void upDateStudent() {

        String id;
        while (true){
            System.out.println("输入id：");
            id=sc.next();
            boolean b=studentService.isExists(id);
            if (!b){
                System.out.println("id不存在，重新输入：");
            }else {
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        String age = sc.next();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        Person newStu = new Student();
        newStu.setId(id);
        newStu.setName(name);
        newStu.setAge(age);
        newStu.setBirthday(birthday);
        studentService.updateStudent(id, newStu);
        System.out.println("修改成功!");
    }

    public void findAllStudent() {
       Person[] people=  studentService.findAllStudent();
       if (people==null){
           System.out.println("暂无数据，请添加：");
       }else{
           System.out.println("学号\t姓名\t年龄\t生日");
           for (int i = 0; i < people.length; i++) {
               Person p=people[i];
               if (p!=null) {
                   System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge() + "\t" + p.getBirthday());
               }
           }
       }
    }

    public void AddStudent() {
        String id;
        while (true){
            System.out.println("输入id：");
             id=sc.next();
             boolean b=studentService.isExists(id);
             if (b){
                 System.out.println("id已存在，重新输入：");
             }else {
                 break;
             }
        }

        System.out.println("输入姓名：");
        String name = sc.next();
        System.out.println("输入年龄：");
        String age = sc.next();
        System.out.println("输入生日：");
        String birthday = sc.next();
        Person person=new Student();
        person.setId(id);
        person.setName(name);
        person.setAge(age);
        person.setBirthday(birthday);
        boolean b=studentService.AddStudent(person);
        if(b){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
}
